<?php

require_once('init.php');

checkAdminLogin();

if($var=request($_REQUEST['act'])) {
  $var = 'act_' . $var;
  if(function_exists($var)) $var();
}
elseif($var=request($_REQUEST['view'])) {
  $var = 'view_' . $var;
  if(function_exists($var)) echo($var());
}

function treeSelect($tree, $id=0, $pid=0, $lev='　') {
  foreach ($tree as $_=>$v) {
    if($v['parent_id']==$id) {
      $newid = $v['type_id'];
      if($pid==$v['type_id']) {
        echo('<option value="'.$v['type_id'].'" selected="true">');
      }
      else {
        echo('<option value="'.$v['type_id'].'">');
      }
      echo($lev.'┠'.$v['type_name'].'</option>');
      treeSelect($tree,$newid,$pid,'　'.$lev);
    }
  }
}
function treeShow($tree, $id=0, $lev='　') {
  global $db;
  foreach ($tree as $_=>$v) {
    if($v['parent_id']==$id) {
      $newid = $v['type_id'];
      echo('<div class="v">');
      echo('  <a href="#">'.$lev.'┠'.$v['type_name'].'</a>');
      echo('   <div class="floatr">');
      echo('    <span class="acticon actedit"><a href="?view=dkptype_edit&id='.$v['type_id'].'">修改</a></span>');
      echo('    <span class="acticon actrecycle"><a onclick="return confirm(\'确定删除?\');" href="?act=dkptype_delete&id='.$v['type_id'].'">删除</a></span>');
      echo('   </div>');
      echo('</div>');
      treeShow($tree,$newid,'　'.$lev);
    }
  }
}

//设置数字颜色
function setcolor($v) {
  if(!$v) return "<span>0.00</span>";
  if($v>0) return "<span class='green'>$v</span>";
  elseif($v<0) return "<span class='red'>$v</span>";
  else return "<span class='gray'>$v</span>";
}

//逗号分割字符串
function splitarray($arr, $key) {
  $str = '';
  foreach($arr as $_=>$v) {
    $str .= "'".$v[$key]."',";
  }
  return substr($str,0,-1);
}

//css&&js
function htmlhead() {
  echo('
    <link rel="stylesheet" type="text/css" href="jscss/global.css" />
    <link rel="stylesheet" type="text/css" href="themes/default/style.css" />
    <script language="javascript" type="text/javascript" src="jscss/global.js"></script>
    <script language="javascript" type="text/javascript" src="jscss/jquery.js"></script>
    <script language="javascript" type="text/javascript" src="jscss/idtabs.js"></script>
    <script language="javascript" type="text/javascript" src="jscss/corner.js"></script>
    <script language="javascript" type="text/javascript" src="jscss/tipsy.js"></script>
    <script language="javascript" type="text/javascript" src="jscss/popup.js"></script>
    <script language="javascript" type="text/javascript" src="../includes/editor/xheditor.js"></script>
    <script language="javascript" type="text/javascript" src="jscss/autocomplete.js"></script>
    <script language="javascript" type="text/javascript" src="jscss/tooltip.js"></script>
    <!--[if IE 6]>
    <link rel="stylesheet" type="text/css" href="themes/default/styleIE6.css" />
    <![endif]-->
  ');
}

//职业
function view_class() {
  global $db;
  $data = $db->getAll("SELECT * FROM @_@classes");
  include('html/class.html');
}
function view_class_edit() {
  global $db;
  $id   = (int)$_REQUEST['id'];
  $data = $db->getOne("SELECT * FROM @_@classes WHERE class_id=$id");
  include('html/class_edit.html');
}
function act_class_add() {
  global $db;
  $arr = array(
    'class_name'  => trim($_POST['class_name']),
    'class_color' => trim($_POST['class_color']),
    'class_pic'   => trim($_POST['class_pic']),
    'class_en'    => strtoupper(trim($_POST['class_en']))
  );
  if(!$arr['class_name']) {
    redirect("?view=class&r=empty");
  }
  if($db->getOne("SELECT * FROM @_@classes WHERE class_name='".$arr['class_name']."'")) {
    redirect("?view=class&r=same");
  }
  $db->query("INSERT INTO @_@classes ".$db->arr_insert($arr));
  redirect("?view=class&r=addok");
}
function act_class_edit() {
  global $db;
  $id  = (int)$_REQUEST['id'];
  $arr = array(
    'class_name'  => trim($_POST['class_name']),
    'class_color' => trim($_POST['class_color']),
    'class_pic'   => trim($_POST['class_pic']),
    'class_en'    => strtoupper(trim($_POST['class_en']))
  );
  if(!$arr['class_name']) {
    redirect("?view=class&r=empty");
  }
  if($db->getOne("SELECT * FROM @_@classes WHERE class_id<>$id AND class_name='".$arr['class_name']."'")) {
    redirect("?view=class&r=same");
  }
  $db->query("UPDATE @_@classes SET ".$db->arr_update($arr)." WHERE class_id=$id");
  redirect("?view=class&r=editok");
}
function act_class_delete() {
  global $db;
  $id = (int)$_REQUEST['id'];
  $db->query("DELETE FROM @_@classes WHERE class_id=$id");
  $db->query("UPDATE @_@members SET member_class=0 WHERE member_class=$id"); //!!
  redirect("?view=class&r=deleteok");
}

//种族
function view_race() {
  global $db;
  $data = $db->getAll("SELECT * FROM @_@races");
  include('html/race.html');
}
function view_race_edit() {
  global $db;
  $id   = (int)$_REQUEST['id'];
  $data = $db->getOne("SELECT * FROM @_@races WHERE race_id=$id");
  include('html/race_edit.html');
}
function act_race_add() {
  global $db;
  $arr = array(
    'race_name'  => trim($_POST['race_name']),
    'race_color' => trim($_POST['race_color']),
    'race_pic'   => trim($_POST['race_pic']),
    'race_en'    => strtoupper(trim($_POST['race_en']))
  );
  if(!$arr['race_name']) {
    redirect("?view=race&r=empty");
  }
  if($db->getOne("SELECT * FROM @_@races WHERE race_name='".$arr['race_name']."'")) {
    redirect("?view=race&r=same");
  }
  $db->query("INSERT INTO @_@races ".$db->arr_insert($arr));
  redirect("?view=race&r=addok");
}
function act_race_edit() {
  global $db;
  $id  = (int)$_REQUEST['id'];
  $arr = array(
    'race_name'  => trim($_POST['race_name']),
    'race_color' => trim($_POST['race_color']),
    'race_pic'   => trim($_POST['race_pic']),
    'race_en'    => strtoupper(trim($_POST['race_en']))
  );
  if(!$arr['race_name']) {
    redirect("?view=race&r=empty");
  }
  if($db->getOne("SELECT * FROM @_@races WHERE race_id<>$id AND race_name='".$arr['race_name']."'")) {
    redirect("?view=race&r=same");
  }
  $db->query("UPDATE @_@races SET ".$db->arr_update($arr)." WHERE race_id=$id");
  redirect("?view=race&r=editok");
}
function act_race_delete() {
  global $db;
  $id = (int)$_REQUEST['id'];
  $db->query("DELETE FROM @_@races WHERE race_id=$id");
  $db->query("UPDATE @_@members SET member_race=0 WHERE member_race=$id"); //!!
  redirect("?view=race&r=deleteok");
}

//级别
function view_rank() {
  global $db;
  $data = $db->getAll("SELECT * FROM @_@ranks");
  include('html/rank.html');
}
function view_rank_edit() {
  global $db;
  $id   = (int)$_REQUEST['id'];
  $data = $db->getOne("SELECT * FROM @_@ranks WHERE rank_id=$id");
  include('html/rank_edit.html');
}
function act_rank_add() {
  global $db;
  $arr = array(
    'rank_name'  => trim($_POST['rank_name']),
    'rank_color' => trim($_POST['rank_color']),
    'rank_pic'   => trim($_POST['rank_pic'])
  );
  if(!$arr['rank_name']) {
    redirect("?view=rank&r=empty");
  }
  if($db->getOne("SELECT * FROM @_@ranks WHERE rank_name='".$arr['rank_name']."'")) {
    redirect("?view=rank&r=same");
  }
  $db->query("INSERT INTO @_@ranks ".$db->arr_insert($arr));
  redirect("?view=rank&r=addok");
}
function act_rank_edit() {
  global $db;
  $id  = (int)$_REQUEST['id'];
  $arr = array(
    'rank_name'  => trim($_POST['rank_name']),
    'rank_color' => trim($_POST['rank_color']),
    'rank_pic'   => trim($_POST['rank_pic'])
  );
  if(!$arr['rank_name']) {
    redirect("?view=rank&r=empty");
  }
  if($db->getOne("SELECT * FROM @_@ranks WHERE rank_id<>$id AND rank_name='".$arr['rank_name']."'")) {
    redirect("?view=rank&r=same");
  }
  $db->query("UPDATE @_@ranks SET ".$db->arr_update($arr)." WHERE rank_id=$id");
  redirect("?view=rank&r=editok");
}
function act_rank_delete() {
  global $db;
  $id = (int)$_REQUEST['id'];
  $db->query("DELETE FROM @_@ranks WHERE rank_id=$id");
  $db->query("UPDATE @_@members SET member_rank=0 WHERE member_rank=$id"); //!!
  redirect("?view=rank&r=deleteok");
}

//副本
function view_instance() {
  global $db;
  $data = $db->getAll("SELECT * FROM @_@instances");
  include('html/instance.html');
}
function view_instance_edit() {
  global $db;
  $id = (int)$_REQUEST['id'];
  $data = $db->getOne("SELECT * FROM @_@instances WHERE instance_id=$id");
  include('html/instance_edit.html');
}
function act_instance_add() {
  global $db;
  $arr = array(
    'instance_gameid' => (int)trim($_POST['instance_gameid']),
    'instance_name'   => trim($_POST['instance_name']),
    'instance_pic'    => trim($_POST['instance_pic']),
    'instance_flag'   => (trim($_POST['instance_flag']) ? strtotime(trim($_POST['instance_flag'])) : 0)
  );
  if(!$arr['instance_name']) {
    redirect("?view=instance&r=empty");
  }
  $db->query("INSERT INTO @_@instances ".$db->arr_insert($arr));
  redirect("?view=instance&r=addok");
}
function act_instance_edit() {
  global $db;
  $id  = (int)$_REQUEST['id'];
  $arr = array(
    'instance_gameid' => (int)trim($_POST['instance_gameid']),
    'instance_name'   => trim($_POST['instance_name']),
    'instance_pic'    => trim($_POST['instance_pic']),
    'instance_flag'   => (trim($_POST['instance_flag']) ? strtotime(trim($_POST['instance_flag'])) : 0)
  );
  if(!$arr['instance_name']) {
    redirect("?view=instance&r=empty");
  }
  $db->query("UPDATE @_@instances SET ".$db->arr_update($arr)." WHERE instance_id=$id");
  redirect("?view=instance&r=editok");
}
function act_instance_delete() {
  global $db;
  $id = (int)$_REQUEST['id'];
  $db->query("DELETE FROM @_@instances WHERE instance_id=$id");
  $db->query("UPDATE @_@bosses SET instance_id=0 WHERE instance_id=$id"); //!!
  redirect("?view=instance&r=deleteok");
}

//BOSS
function view_boss() {
  global $db;
  $instances = $db->getAll("SELECT * FROM @_@instances");
  $data = $db->getAll("SELECT
    a.boss_id AS boss_id,
    a.boss_gameid AS boss_gameid,
    a.boss_name AS boss_name,
    a.boss_flag AS boss_flag,
    a.boss_num AS boss_num,
    a.boss_pic AS boss_pic,
    b.instance_name AS instance_name
    FROM @_@bosses AS a
    LEFT JOIN @_@instances AS b ON a.instance_id=b.instance_id
    ORDER BY boss_id DESC
  ");
  include('html/boss.html');
}
function view_boss_edit() {
  global $db;
  $id = (int)$_REQUEST['id'];
  $instances = $db->getAll("SELECT * FROM @_@instances");
  $data = $db->getOne("SELECT * FROM @_@bosses WHERE boss_id=$id");
  include('html/boss_edit.html');
}
function act_boss_add() {
  global $db;
  $arr = array(
    'instance_id' => trim($_POST['instance_id']),
    'boss_gameid' => trim($_POST['boss_gameid']),
    'boss_name'   => trim($_POST['boss_name']),
    'boss_pic'    => trim($_POST['boss_pic']),
    'boss_flag'   => (trim($_POST['boss_flag']) ? strtotime(trim($_POST['boss_flag'])) : ''),
    'boss_num'    => (int)$_POST['boss_num']
  );
  if(!$arr['instance_id'] or !$arr['boss_name']) {
    redirect("?view=boss&r=empty");
  }
  $db->query("INSERT INTO @_@bosses ".$db->arr_insert($arr));
  redirect("?view=boss&r=addok");
}
function act_boss_edit() {
  global $db;
  $id  = (int)$_REQUEST['id'];
  $arr = array(
    'instance_id' => trim($_POST['instance_id']),
    'boss_gameid' => trim($_POST['boss_gameid']),
    'boss_name'   => trim($_POST['boss_name']),
    'boss_pic'    => trim($_POST['boss_pic']),
    'boss_flag'   => (trim($_POST['boss_flag']) ? strtotime(trim($_POST['boss_flag'])) : ''),
    'boss_num'    => (int)$_POST['boss_num']
  );
  if(!$arr['instance_id'] or !$arr['boss_name']) {
    redirect("?view=boss_edit&id=$id&r=empty");
  }
  $db->query("UPDATE @_@bosses SET ".$db->arr_update($arr)." WHERE boss_id=$id");
  redirect("?view=boss&r=editok");
}
function act_boss_delete() {
  global $db;
  $id = (int)$_REQUEST['id'];
  $db->query("DELETE FROM @_@bosses WHERE boss_id=$id");
  redirect("?view=boss&r=deleteok");
}

//NEWS
function view_news_add() {
  include('html/news_add.html');
}
function view_news_edit() {
  global $db;
  $id = (int)$_REQUEST['id'];
  $data = $db->getOne("SELECT * FROM @_@articles WHERE article_id=$id");
  include('html/news_edit.html');
}
function view_news_list() {
  global $db;
  list($navi,$data) = $db->pagination("SELECT * FROM @_@articles ORDER BY article_rank DESC, article_id DESC",$_REQUEST['page'],$_REQUEST['size']);
  include('html/news_list.html');
}
function act_news_add() {
  global $db;
  $arr = array(
    'article_title'   => trim($_POST['article_title']),
    'article_content' => trim($_POST['article_content']),
    'article_time'    => strtotime(trim($_POST['article_time'])),
    'article_rank'    => (int)$_POST['article_rank'],
    'user_name'       => decrypt($_SESSION[AUTH.'_admin']['user_name'])
  );
  if(!$arr['article_title']) {
    redirect("?view=news_add&r=empty");
  }
  $db->query("INSERT INTO @_@articles ".$db->arr_insert($arr));
  redirect("?view=news_add&r=addok");
}
function act_news_edit() {
  global $db;
  $id  = (int)$_REQUEST['id'];
  $arr = array(
    'article_title'   => trim($_POST['article_title']),
    'article_content' => trim($_POST['article_content']),
    'article_time'    => strtotime(trim($_POST['article_time'])),
    'article_rank'    => (int)$_POST['article_rank'],
    'user_name'       => decrypt($_SESSION[AUTH.'_admin']['user_name'])
  );
  if(!$arr['article_title']) {
    redirect("?view=news_edit&id=$id&r=empty");
  }
  $db->query("UPDATE @_@articles SET ".$db->arr_update($arr)." WHERE article_id=$id");
  redirect("?view=news_list&r=editok");
}
function act_news_delete() {
  global $db;
  $id  = (int)$_REQUEST['id'];
  $ids = $_REQUEST['ids'];
   if($ids) {
    $db->query("DELETE FROM @_@articles WHERE article_id IN ($ids)");
  }
  else {
    $db->query("DELETE FROM @_@articles WHERE article_id=$id");
  }
  redirect("?view=news_list&r=deleteok");
}

//MEMBER
function view_member_add() {
  global $db;
  $ranks = $db->getAll("SELECT * FROM @_@ranks");
  $races = $db->getAll("SELECT * FROM @_@races");
  $classes = $db->getAll("SELECT * FROM @_@classes");
  $members = $db->getAll("SELECT member_id,member_name FROM @_@members WHERE parent_id=0");
  include('html/member_add.html');
}
function view_member_list() {
  global $db;
  list($navi,$data) = $db->pagination("SELECT
    a.member_id AS member_id,
    a.user_id AS user_id,
    a.parent_id AS parent_id,
    a.member_name AS member_name,
    a.member_level AS member_level,
    a.firstraid AS firstraid,
    a.lastraid AS lastraid,
    a.raidcount AS raidcount,
    b.race_name AS member_race,
    c.class_name AS member_class,
    d.rank_name AS member_rank
    FROM @_@members AS a
    LEFT JOIN @_@races AS b ON a.member_race=b.race_id
    LEFT JOIN @_@classes AS c ON a.member_class=c.class_id
    LEFT JOIN @_@ranks AS d ON a.member_rank=d.rank_id
    WHERE a.user_id=".$_SESSION[AUTH.'_admin']['user_id'],$_REQUEST['page'],$_REQUEST['size']);
  include('html/member_list.html');
}
function view_member_edit() {
  global $db;
  $id = (int)$_REQUEST['id'];
  $data = $db->getOne("SELECT * FROM @_@members WHERE member_id=$id");
  $ranks = $db->getAll("SELECT * FROM @_@ranks");
  $races = $db->getAll("SELECT * FROM @_@races");
  $classes = $db->getAll("SELECT * FROM @_@classes");
  $members = $db->getAll("SELECT member_id,member_name FROM @_@members WHERE parent_id=0");
  include('html/member_edit.html');
}
function act_member_add() {
  global $db;
  $arr = array(
    'member_name'   => ucfirst(strtolower(trim($_POST['member_name']))),
    'member_race'   => (int)$_POST['member_race'],
    'member_class'  => (int)$_POST['member_class'],
    'member_level'  => (int)$_POST['member_level'],
    'member_rank'   => (int)$_POST['member_rank'],
    'parent_id'     => (int)$_POST['parent_id'],
    'user_id'       => $_SESSION[AUTH.'_admin']['user_id']
  );
  if(!$arr['member_name']) {
    redirect("?view=member_add&r=empty");
  }
  $db->query("INSERT INTO @_@members ".$db->arr_insert($arr));
  redirect("?view=member_add&r=addok");
}
function act_member_edit() {
  global $db;
  $id  = (int)$_REQUEST['id'];
  $arr = array(
    'member_name'   => ucfirst(strtolower(trim($_POST['member_name']))),
    'member_race'   => (int)$_POST['member_race'],
    'member_class'  => (int)$_POST['member_class'],
    'member_level'  => (int)$_POST['member_level'],
    'member_rank'   => (int)$_POST['member_rank'],
    'parent_id'     => (int)$_POST['parent_id']
  );
  if(!$arr['member_name']) {
    redirect("?view=member_edit&id=$id&r=empty");
  }
  if($id==$arr['parent_id']) {
    redirect("?view=member_edit&id=$id&r=error");
  }
  $db->query("UPDATE @_@members SET ".$db->arr_update($arr)." WHERE member_id=$id");
  redirect("?view=member_list&r=editok");
}
function act_member_delete() {
  global $db;
  $id  = (int)$_REQUEST['id'];
  $ids = $_REQUEST['ids'];
  if($ids) {
    $db->query("DELETE FROM @_@members WHERE member_id IN ($ids)");
    $db->query("DELETE FROM @_@dkplist WHERE member_id IN ($ids)"); //!!
  }
  else {
    $db->query("DELETE FROM @_@members WHERE member_id=$id");
    $db->query("DELETE FROM @_@dkplist WHERE member_id=$id"); //!!
  }
  redirect("?view=member_list&r=deleteok");
}

//DKPTYPE
function view_dkptype() {
  global $db;
  $data = $db->getAll("SELECT * FROM @_@dkptype");
  include('html/dkptype.html');
}
function view_dkptype_edit() {
  global $db;
  $id   = (int)$_REQUEST['id'];
  $data = $db->getOne("SELECT * FROM @_@dkptype WHERE type_id=$id");
  $treedata = $db->getAll("SELECT * FROM @_@dkptype");
  include('html/dkptype_edit.html');
}
function act_dkptype_add() {
  global $db;
  $parent_id = (int)$_POST['parent_id'];
  $type_path = '0,';
  $type_name = trim($_POST['type_name']);
  if(!$type_name) { redirect("?view=dkptype&r=empty"); }
  if($parent_id!=0) {
    if($rez=$db->getOne("SELECT * FROM @_@dkptype WHERE type_id=$parent_id")) {
      $type_path = $rez['type_path'] . $parent_id . ',';
    }
  }
  $db->query("INSERT INTO @_@dkptype (parent_id,type_path,type_name) VALUES ($parent_id,'$type_path','$type_name') ");
  redirect("?view=dkptype&r=addok");
}
function act_dkptype_edit() {
  global $db;
  $id  = (int)$_REQUEST['id'];
  $type_path = '0,';
  $parent_id = (int)$_REQUEST['parent_id'];
  $type_name = trim($_POST['type_name']);
  if(!$type_name) { redirect("?view=dkptype_edit&id=$id&r=empty"); }
  if($id==$parent_id) { redirect("?view=dkptype_edit&id=$id&r=self");}
  $rezold  = $db->getOne("SELECT * FROM @_@dkptype WHERE type_id=$id");
  $oldpath = $rezold['type_path'] . $rezold['type_id'] . ',';
  if($reznew  = $db->getOne("SELECT * FROM @_@dkptype WHERE type_id=$parent_id")) {
    $type_path = $reznew['type_path'] . $parent_id . ',';
    $newpath = $type_path . $id . ',';
    if(preg_match("/".$oldpath."/", $newpath)) {
      redirect("?view=dkptype_edit&id=$id&r=self");
    }
  }
  else {
    $newpath = '0,' . $id . ',';
  }
  $db->query("UPDATE @_@dkptype SET parent_id=$parent_id, type_path='$type_path', type_name='$type_name' WHERE type_id=$id");
  redirect("?view=dkptype&r=editok");
}
function act_dkptype_delete() {
  global $db;
  $id = (int)$_REQUEST['id'];
  $db->query("DELETE FROM @_@dkptype WHERE type_id=$id");
  $db->query("DELETE FROM @_@dkplist WHERE type_id=$id"); //!!
  redirect("?view=dkptype&r=deleteok");
}

//DKPLIST
function view_dkplist() {
  global $db;
  $pid    = (int)$_REQUEST['pid'];
  $classid = (int)$_REQUEST['classid'];
  $classes = $db->getAll("SELECT * FROM @_@classes");
  $dkptypes = $db->getAll("SELECT * FROM @_@dkptype");  
  if($classid) {
    $condi = " b.member_class=$classid ";
  }
  else {
    $condi = ' 1=1 ';
  }
  $sql = "SELECT
    a.type_id AS type_id,
    a.member_id AS member_id,
    a.dkp_earn AS dkp_earn,
    a.dkp_spent AS dkp_spent,
    a.dkp_adjust AS dkp_adjust,
    a.dkp_base AS dkp_base,
    b.member_name AS member_name,
    b.member_level AS member_level,
    b.raidcount AS raidcount,
    b.firstraid AS firstraid,
    b.lastraid AS lastraid,
    c.race_name AS member_race,
    d.class_name AS member_class,
    e.rank_name AS member_rank
    FROM @_@dkplist AS a
    LEFT JOIN @_@members AS b ON b.member_id=a.member_id
    LEFT JOIN @_@races AS c ON c.race_id=b.member_race
    LEFT JOIN @_@classes AS d ON d.class_id=b.member_class 
    LEFT JOIN @_@ranks AS e ON e.rank_id=b.member_rank ";

  if($pid!=0) {
    $pids = $pid . ',';
    if($rez = $db->getAll("SELECT type_id FROM @_@dkptype WHERE type_path LIKE '%$pid,%'")) {      
      foreach($rez as $_=>$v) {
        $pids .= $v['type_id'] . ',';
      }
    }
    $pids = str_replace("0,","",substr($pids,0,-1));
    list($navi,$data) = $db->pagination($sql." WHERE $condi AND a.type_id IN ($pids)",$_REQUEST['page'],$_REQUEST['size']);
    $total = count($db->getAll("SELECT event_id FROM @_@events WHERE type_id IN ($pids)"));
  }
  else {
    $navi  = array();
    $data  = array();
    $total = 1;
    //list($navi,$data) = $db->pagination($sql,$_REQUEST['page'],$_REQUEST['size']);
  }
  include('html/dkplist.html');
}
function act_dkplist_adjust() {
  global $db;
  $id  = (int)$_REQUEST['id'];
  $pid = (int)$_REQUEST['pid'];
  $ids = trim($_REQUEST['ids']);
  if(!$pid) exit('please choose a dkptype');
  if($ids) {
    
  }
  else {
    
  }
  
}

//PARSER XML
function act_report_parser() {
  global $db;
  switch(strtoupper(trim($_REQUEST['datatype']))) {
    case "CTRT":
      $report = C('Parser_report')->parser_ctrt($_POST['content']);
      break;
    case "MERDKP":
      $report = C('Parser_report')->parser_merdkp($_POST['content']);
      break;
    case "MIDKP":
      $report = C('Parser_report')->parser_midkp($_POST['content']);
      break;
    case "REPORT":
      $report = C('Parser_report')->parser_report($_POST['content']);
      break;
    case "TEXT":
      $report = C('Parser_report')->parser_text($_POST['content']);
      break;
    default:
      redirect("?view=report_parser&r=error");
  }
  $dkptypes  = $db->getAll("SELECT * FROM @_@dkptype");
  $instances = $db->getAll("SELECT * FROM @_@instances ORDER BY instance_name");
  $classes = $db->getAll("SELECT * FROM @_@classes");
  $races = $db->getAll("SELECT * FROM @_@races");
  include('html/report_add.html');
}
function act_report_add() {
  global $db;
  $instance_name = trim($_POST['instance_name']);
  if(!$instance_name) exit('please input an instance_name');
  if($rez=$db->getOne("SELECT * FROM @_@instances WHERE instance_name='$instance_name'")) {
    $instance_id = $rez['instance_id'];
    $t_instance = false;
  }
  else {
    $db->query("INSERT INTO @_@instances (instance_name) VALUES ('$instance_name')");
    $instance_id = $db->insert_id();
    $t_instance = true;
  }
  
  
  $savedb = array(
    'version'  => $_POST['version'],
    'addon'    => $_POST['addon'],
    'name'     => trim($_POST['event_name']),
    'start'    => strtotime($_POST['event_start']),
    'end'      => strtotime($_POST['event_end']),
    'note'     => trim($_POST['event_note']),
    'instance' => trim($_POST['instance_name']),
    'members'  => $members,
    'events'   => $events,
    'items'    => $items
  );
  
  $arr = array(
    'type_id'      => (int)$_POST['type_id'],
    'instance_id'  => $instance_id,
    'event_name'   => trim($_POST['event_name']),
    'event_start'  => strtotime($_POST['event_start']),
    'event_end'    => strtotime($_POST['event_end']),
    'event_note'   => trim($_POST['event_note']),
    'event_source' => serialize($savedb),
    'posttime'     => time(),
    
  );
  
  //$db->query("INSERT INTO @_@events ".$db->arr_insert($arr));
  
}

function get_memberrace($races,$race) {
  foreach($races as $_=>$v) {
    if($v['race_name']==$race || $v['race_en']==$race) return $v['race_id'];
  }
  return 0;
}
function get_memberclass($classes,$class) {
  foreach($classes as $_=>$v) {
    if($v['class_name']==$class || $v['class_en']==$class) return $v['class_id'];
  }
  return 0;
}
function view_report_autoparser() {
  global $db;
  $dkptypes  = $db->getAll("SELECT * FROM @_@dkptype");
  $instances = $db->getAll("SELECT * FROM @_@instances ORDER BY instance_name");
  include('html/report_autoparser.html');
}
function act_report_autoparser() {
  global $db;
  $type_id = (int)$_POST['type_id'];
  $instance_name = trim($_POST['instance_name']);
  $event_name = trim($_POST['event_name']);
  switch(strtoupper(trim($_REQUEST['datatype']))) {
    case "CTRT":
      $report = C('Parser_report')->parser_ctrt($_POST['content']);
      break;
    case "MERDKP":
      $report = C('Parser_report')->parser_merdkp($_POST['content']);
      break;
    case "MIDKP":
      $report = C('Parser_report')->parser_midkp($_POST['content']);
      break;
    case "REPORT":
      $report = C('Parser_report')->parser_report($_POST['content']);
      break;
    case "TEXT":
      $report = C('Parser_report')->parser_text($_POST['content']);
      break;
    default:
      redirect("?view=report_parser&r=error");
  }
  
  if(!$instance_name) $instance_name = $report['instance'];
  if(!$instance_name) exit('please input an instance_name');
  if($rez=$db->getOne("SELECT * FROM @_@instances WHERE instance_name='$instance_name'")) {
    $instance_id = $rez['instance_id'];
  }
  else {
    $db->query("INSERT INTO @_@instances (instance_name) VALUES ('$instance_name')");
    $instance_id = $db->insert_id();
  }
  
  if(!$event_name) $event_name = $report['name'];
  if(!$event_name) $event_name = date("Y-m-d H:i:s",$report['start']);
  
  //更新到events表  
  $arr_event = array(
    'type_id'      => $type_id,
    'instance_id'  => $instance_id,
    'event_name'   => $event_name,
    'event_start'  => $report['start'],
    'event_end'    => $report['end'],
    'event_note'   => $report['note'],
    'event_source' => '', //serialize($report),
    'posttime'     => time()
  );
  $db->query("INSERT INTO @_@events ".$db->arr_insert($arr_event));
  $event_id = $db->insert_id();
  //缓存到文件
  //C('Cache',array('cache_file'=>$event_id.'.txt'))->setcache($report,true);
  C('Cache',array('json_file'=>$event_id.'.json'))->savejson($report);
  
  $classes = $db->getAll("SELECT * FROM @_@classes");
  $races = $db->getAll("SELECT * FROM @_@races");

  //更新到items表 同时计算花费分数
  $spents = array();
  foreach($report['items'] as $_=>$v) {
    list($color,$itemid,$itemname) = explode("|",$v[0]);
    $db->query("INSERT INTO @_@items ".$db->arr_insert(array(
      'event_id'    => $event_id,
      'item_id'     => $itemid,
      'item_name'   => $itemname,
      'item_color'  => $color,
      'item_looter' => $v[3],
      'item_score'  => $v[1],
      'item_count'  => $v[2],
      'loot_time'   => $v[4]
    )));
    if($spents[$v[3]]) {
      $spents[$v[3]] += $v[1] * $v[2];
    }
    else {
      $spents[$v[3]] = $v[1] * $v[2];
    }
  }
  
  //计算获得分数
  $earns = array();
  foreach($report['events'] as $_=>$v) {
    foreach($v[4] as $mn) {
      if($earns[$mn]) {
        $earns[$mn] += $v[1];
      }
      else {
        $earns[$mn] = $v[1];
      }
    }
    if($v[2]) {
      if($b=$db->getOne("SELECT * FROM @_@bosses WHERE boss_name='".$v[2]."'")) {
        if(!$b['boss_flag']) $b_b = ", boss_flag='".$v[3]."' ";
        else $b_b = '';
        $db->query("UPDATE @_@bosses SET boss_num=boss_num+1$b_b WHERE boss_id=".$b['boss_id']);
      }
      else {
        $db->query("INSERT INTO @_@bosses ".$db->arr_insert(array(
          'instance_id' => $instance_id,
          'boss_name'   => $v[2],
          'boss_flag'   => $v[3],
          'boss_num'    => 1
        )));
      }
    }
  }
  
  //更新到members表,dkplist表,growing表
  foreach($report['members'] as $_=>$v) {
    if( $m = $db->getOne("SELECT * FROM @_@members WHERE member_name='" . $v[0] . "'") ) {
      if(!$m['member_race']) $db->query("UPDATE @_@members SET member_race=".get_memberrace($races,$v[1])." WHERE member_id=".$m['member_id']);
      if(!$m['member_class']) $db->query("UPDATE @_@members SET member_class=".get_memberclass($classes,$v[2])." WHERE member_id=".$m['member_id']);
      $db->query("UPDATE @_@members SET lastraid=".$report['start'].", raidcount=raidcount+1 WHERE member_id=".$m['member_id']);
      $member_id = $m['member_id'];
    }
    else {
      $db->query("INSERT INTO @_@members ".$db->arr_insert(array(
        'member_name'   => $v[0],
        'member_race'   => get_memberrace($races,$v[1]),
        'member_class'  => get_memberclass($classes,$v[2]),
        'member_level'  => $v[3],
        'firstraid'     => $report['start'],
        'lastraid'      => $report['start'],
        'raidcount'     => 1
      )));
      $member_id = $db->insert_id();
    }

    if($db->getOne("SELECT * FROM @_@dkplist WHERE type_id=$type_id AND member_id=$member_id")) {
      $db->query("UPDATE @_@dkplist SET dkp_earn=dkp_earn+".round($earns[$v[0]],2).", dkp_spent=dkp_spent+".round($spents[$v[0]],2)."  WHERE type_id=$type_id AND member_id=$member_id");
    }
    else {
      $db->query("INSERT INTO @_@dkplist (type_id,member_id,dkp_earn,dkp_spent) VALUES ($type_id,$member_id,'".round($earns[$v[0]],2)."','".round($spents[$v[0]],2)."')");
    }
    
    $db->query("INSERT INTO @_@growing ".$db->arr_insert(array(
      'type_id'   => $type_id,
      'member_id' => $member_id,
      'event_id'  => $event_id,
      'earn'      => round($earns[$v[0]],2),
      'spent'     => round($spents[$v[0]],2)
    )));
    
  }
  
  $dkptypes  = $db->getAll("SELECT * FROM @_@dkptype");
  include('html/report_view.html');
}

//events
function view_event_list() {
  global $db;
  $sql = "SELECT 
    b.type_name AS type_name,
    c.instance_name AS instance_name,
    a.event_name AS event_name,
    a.event_start AS event_start,
    a.event_end AS event_end,
    a.event_note AS event_note,
    a.posttime AS posttime 
    FROM @_@events AS a
    LEFT JOIN @_@dkptype AS b ON b.type_id=a.type_id 
    LEFT JOIN @_@instances AS c ON c.instance_id=a.instance_id 
    ORDER BY a.event_id DESC
  ";
  list($navi,$data) = $db->pagination($sql,$_REQUEST['page'],$_REQUEST['size']);
  include('html/eventlist.html');
}

//items
function view_item_list() {
  global $db;
  $sql = "SELECT 
    a.event_id AS event_id,
    a.item_id AS item_id,
    a.item_name AS item_name,
    a.item_color AS item_color,
    a.item_looter AS item_looter,
    a.item_score AS item_score,
    a.item_count AS item_count,
    a.loot_time AS loot_time,
    b.event_name AS event_name 
    FROM @_@items AS a
    LEFT JOIN @_@events AS b ON b.event_id=a.event_id 
  ";
  list($navi,$data) = $db->pagination($sql,$_REQUEST['page'],$_REQUEST['size']);
  include('html/itemlist.html');
}

//growing
function view_growing() {
  global $db;
  $sql = "SELECT 
    a.earn AS earn,
    a.spent AS spent,
    b.type_name AS type_name,
    c.event_name AS event_name,
    d.member_name AS member_name 
    FROM @_@growing AS a 
    LEFT JOIN @_@dkptype AS b ON b.type_id=a.type_id 
    LEFT JOIN @_@events AS c ON c.event_id=a.event_id 
    LEFT JOIN @_@members AS d ON d.member_id=a.member_id 
  ";
  list($navi,$data) = $db->pagination($sql,$_REQUEST['page'],$_REQUEST['size']);
  include('html/growing.html');
}


//tooltips
function act_tip() {
  $type = strtolower(trim($_REQUEST['type']));
  $id   = (int)$_REQUEST['id'];
  switch($type) {
    case 'item':
      echo C('Armory')->get_item($id);
      break;
    default:
      echo('error:type');
  }
}

//delete caches
function act_clearcaches() {
  $directory = '../caches/themes';
  $mydir = dir($directory);
  while($file = $mydir->read()){
    if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!="..")){
      del_dir("$directory/$file");
    }
    else {
      if(($file!=".") AND ($file!="..")){
        unlink("$directory/$file");
      }
    }
  }
  $mydir->close();
  redirect('start.php',2,'已清理完毕,2秒后自动跳转...');
}

//reset pwd
function view_resetpwd() {
  include('html/resetpwd.html');
}
function act_resetpwd() {
  global $db;
  $oldpwd = md5(trim($_POST['oldpwd']));
  $newpwd = trim($_POST['newpwd']);
  $repwd  = trim($_POST['repwd']);
  if(!$newpwd || !$repwd) {
    redirect('?view=resetpwd',1,'密码不能为空');
  }
  if($newpwd!=$repwd) {
    redirect('?view=resetpwd',1,'两次密码不同');
  }
  if($oldpwd!=$_SESSION[AUTH.'_admin']['user_pwd']) {
    redirect('?view=resetpwd',1,'旧密码不匹配');
  }
  $db->query("UPDATE @_@users SET user_pwd='".md5($newpwd)."' WHERE user_id=".$_SESSION[AUTH.'_admin']['user_id']);
  $_SESSION[AUTH.'_admin']['user_pwd'] = md5($newpwd);
  redirect('?view=resetpwd&r=ok');
}

//backup
function act_backup() {
  global $db;
  file_down($db->backup());
}

?>
